home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / mxlibs / dwstk102 / playdwd.c < prev    next >
C/C++ Source or Header  |  1995-04-12  |  5KB  |  188 lines

  1. /******************************************************************************
  2. File:          playdwd.c
  3. Version:     1.02
  4. Tab stops: every 2 columns
  5. Project:     DWD Player
  6. Copyright: 1994-1995 DiamondWare, Ltd.    All rights reserved.
  7. Written:     Keith Weiner & Erik Lorenzen
  8. Purpose:     Contains simple example code to show how to load/play a .DWD file
  9. History:     94/10/21 KW Started
  10.                      95/02/21 EL Finalized
  11.                      95/03/22 EL Finalized for 1.01
  12.                      95/04/11 EL Finalized for 1.02
  13.  
  14. Notes
  15. -----
  16. This code isn't really robust when it comes to standard error checking
  17. and particularly recovery, software engineering technique, etc.  A buffer
  18. is statically allocated.    A better technique would be to use fstat() or stat()
  19. to determine the file's size then malloc(size).  The STK will handle songs
  20. larger than 64K (but not digitized sounds).  Obviously, you'd need to fread()
  21. such a file in chunks, or write some sort of hfread() (huge fread).  Also,
  22. exitting and cleanup is not handled robustly in this code.    The code below can
  23. only be validated by extremely careful scrutiny to make sure each case is
  24. handled properly.  A better method would the use of C's atexit function.
  25.  
  26. But all such code would make this example file less clear; its purpose was
  27. to illustrate how to call the STK, not how to write QA-proof software.
  28. ******************************************************************************/
  29.  
  30.  
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <conio.h>
  35.  
  36. #include "dws.h"
  37. #include "err.h"
  38.  
  39.  
  40.  
  41. #define BUFFSIZE 65535        /* If the linker outputs the error */
  42.                                                     /* "stack plus data exceed 64K" */
  43.                                                     /* try reducing BUFFSIZE to about 32K, */
  44.                                                     /* or compile for large model. */
  45. byte _far sound[BUFFSIZE];
  46.  
  47.  
  48.  
  49. void main(int argc, char **argv)
  50. {
  51.     FILE                                *fp;
  52.     dws_DETECTOVERRIDES dov;
  53.     dws_DETECTRESULTS     dres;
  54.     dws_IDEAL                     ideal;
  55.     dws_DPLAY                     dplay;
  56.     word                                result;
  57.  
  58.     printf("\nPLAYDWD 1.02 is Copyright 1994-95 DiamondWare, Ltd.\n");
  59.     printf("All rights reserved.\n\n\n");
  60.  
  61.     if (argc < 2)
  62.     {
  63.         printf("Usage PLAYDWD <dwd-file> \n");
  64.         exit(-1);
  65.     }
  66.  
  67.     fp = fopen(argv[1], "rb");
  68.  
  69.     if (fp == NULL)
  70.     {
  71.         printf("Unable to open %s\n", argv[1]);
  72.         exit(-1);
  73.     }
  74.  
  75.     fread(sound, (size_t)BUFFSIZE, 1, fp);/* if filelen < BUFFSIZE, this works */
  76.  
  77.     fclose(fp);
  78.  
  79.     /*
  80.      . We need to set every field to -1 in dws_DETECTOVERRIDES struct;
  81.      . this tells the STK to autodetect everything.  Any other value
  82.      . overrides the autodetect routine, and will be accepted on
  83.      . faith, though the STK will verify it if possible.
  84.     */
  85.     dov.baseport = (word)-1;
  86.     dov.digdma     = (word)-1;
  87.     dov.digirq     = (word)-1;
  88.  
  89.     if (!dws_DetectHardWare(&dov, &dres))
  90.     {
  91.         err_Display(dws_ErrNo());
  92.     }
  93.  
  94.     if (!(dres.capability & dws_capability_DIG))
  95.     {
  96.         if ((dres.baseport != 0x388) && (dres.baseport != (word)-1))
  97.         {
  98.             printf("The sound hardware supports digitized sound playback.\n");
  99.             printf("We couldn't find the DMA channel and/or IRQ level.\n");
  100.         }
  101.         else
  102.         {
  103.             printf("DIG support not found\n");
  104.         }
  105.  
  106.         exit(-1);
  107.     }
  108.  
  109.     /*
  110.      . The "ideal" struct tells the STK how you'd like it to initialize the
  111.      . sound hardware.    In all cases, if the hardware won't support your
  112.      . request, the STK will go as close as possible.  For example, not all
  113.      . sound boards will support all sampling rates (some only support five or
  114.      . six discrete rates).
  115.     */
  116.     ideal.musictyp     = 0;      /* 0=No music, 1=OPL2 */
  117.     ideal.digtyp         = 8;      /* 0=No Dig, 8=8bit */
  118.     ideal.digrate      = 5000; /* sampling rate, in Hz */
  119.                                                      /* we could have called dws_DGetRateFromDWD */
  120.                                                      /* before initting the STK to get the rate */
  121.     ideal.dignvoices = 16;     /* number of voices (up to 16) */
  122.     ideal.dignchan     = 1;      /* 1=mono */
  123.  
  124.     if (!dws_Init(&dres, &ideal))
  125.     {
  126.         err_Display(dws_ErrNo());
  127.         exit(-1);
  128.     }
  129.  
  130.     /* Set Master Volume to about 80% max */
  131.     if (!dws_XMaster(200))
  132.     {
  133.         err_Display(dws_ErrNo());
  134.     }
  135.  
  136.     dplay.snd          = sound;
  137.     dplay.count      = 1;          /* 0=infinite loop, else num times to play sound */
  138.     dplay.priority = 1000;
  139.     dplay.presnd     = 0;
  140.  
  141.     if (!dws_DGetRateFromDWD(dplay.snd, &ideal.digrate))
  142.     {
  143.         err_Display(dws_ErrNo());
  144.         goto KillIt;
  145.     }
  146.  
  147.     if (!dws_DSetRate(ideal.digrate))
  148.     {
  149.         err_Display(dws_ErrNo());
  150.         goto KillIt;
  151.     }
  152.  
  153.     if (!dws_DPlay(&dplay))
  154.     {
  155.         err_Display(dws_ErrNo());
  156.         goto KillIt;
  157.     }
  158.  
  159.     do
  160.     {
  161.         if (!dws_DSoundStatus(dplay.soundnum, &result))
  162.         {
  163.             err_Display(dws_ErrNo());
  164.             goto KillIt;
  165.         }
  166.  
  167.     } while (result);
  168.  
  169.  
  170.     KillIt:
  171.  
  172.     if (!dws_Kill())
  173.     {
  174.         /*
  175.          . If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  176.          . or dws_NOTINITTED.  If it's dws_Kill_CANTUNHOOKISR the user
  177.          . must remove his tsr, and dws_Kill must be called again.    If it's
  178.          . dws_NOTINITTED, there's nothing to worry about at this point.
  179.         */
  180.         err_Display(dws_ErrNo());
  181.  
  182.         if (dws_ErrNo() == dws_Kill_CANTUNHOOKISR)
  183.         {
  184.             goto KillIt;
  185.         }
  186.     }
  187. }
  188.